package com.example.meituan.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import com.example.meituan.activity.bean.OrderDetailsBean;
import com.example.meituan.db.DBUntil;

public class OrderDetailsDao {

    private static SQLiteDatabase db=DBUntil.con;



    // 添加订单详情
    public long addOrderDetail(OrderDetailsBean orderDetail) {
        ContentValues values = new ContentValues();
        values.put("s_id", orderDetail.getId());
        values.put("s_shop_id", orderDetail.getShopId());
        values.put("s_name", orderDetail.getName());
        values.put("s_des", orderDetail.getDescription());
        values.put("s_price", orderDetail.getPrice());
        values.put("s_num", orderDetail.getNum());
        values.put("s_send_id", orderDetail.getSendId());
        values.put("s_detial_type", orderDetail.getDetailType());
        values.put("s_img", orderDetail.getImg());

        return db.insert("d_order_detials_id", null, values);
    }

    // 删除订单详情
    public int deleteOrderDetailById(String id) {
        return db.delete("d_order_detials_id", "s_id=?", new String[]{id});
    }

    // 更新订单详情
    public int updateOrderDetail(OrderDetailsBean orderDetail) {
        ContentValues values = new ContentValues();
        values.put("s_shop_id", orderDetail.getShopId());
        values.put("s_name", orderDetail.getName());
        values.put("s_des", orderDetail.getDescription());
        values.put("s_price", orderDetail.getPrice());
        values.put("s_num", orderDetail.getNum());
        values.put("s_send_id", orderDetail.getSendId());
        values.put("s_detial_type", orderDetail.getDetailType());
        values.put("s_img", orderDetail.getImg());

        return db.update("d_order_detials_id", values, "s_id=?", new String[]{orderDetail.getId()});
    }

    // 查询所有订单详情
    public List<OrderDetailsBean> getAllOrderDetails() {
        List<OrderDetailsBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_order_detials_id", null);

        try {
            while (cursor.moveToNext()) {
                OrderDetailsBean orderDetail = createOrderDetailFromCursor(cursor);
                list.add(orderDetail);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 根据 ID 查询单个订单详情
    public static OrderDetailsBean getOrderDetailById(String id) {
        Cursor cursor = db.rawQuery("select * from d_order_detials_id where s_id=?", new String[]{id});
        OrderDetailsBean orderDetailsBean=new OrderDetailsBean();
        try {
            if (cursor.moveToNext()) {
                @SuppressLint("Range") String shopId=cursor.getString(cursor.getColumnIndex("s_shop_id"));
                @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
                @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
                @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
                @SuppressLint("Range") String num=cursor.getString(cursor.getColumnIndex("s_num"));
                @SuppressLint("Range") String sendId=cursor.getString(cursor.getColumnIndex("s_send_id"));
                @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_detial_type"));
                @SuppressLint("Range") String img=cursor.getString(cursor.getColumnIndex("s_img"));
                orderDetailsBean.setImg(img);
                orderDetailsBean.setNum(num);
                orderDetailsBean.setDetailType(type);
                orderDetailsBean.setId(id);
                orderDetailsBean.setName(name);
                orderDetailsBean.setDescription(des);
                orderDetailsBean.setPrice(price);
                orderDetailsBean.setSendId(sendId);
                orderDetailsBean.setShopId(shopId);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return orderDetailsBean;
    }

    // 辅助方法：从游标创建订单详情对象
    private OrderDetailsBean createOrderDetailFromCursor(Cursor cursor) {
        OrderDetailsBean orderDetail = new OrderDetailsBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_shop_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_name")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_des")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_price")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_num")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_send_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_detial_type")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_img"))
        );
        return orderDetail;
    }
}
